package com.wh.order.controller;

import com.wh.order.service.IOrderDetailService;
import com.wh.order.pojo.OrderDetail;
import com.wh.order.dto.OrderDetailDto;
import com.wh.basic.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/orderDetail")
public class OrderDetailController {

    @Autowired
    public IOrderDetailService orderDetailService;

    /**
     * 接口：添加或修改
     * @param orderDetail 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderDetail orderDetail) {
        if ( orderDetail.getId() != null){
            orderDetailService.updateById(orderDetail);
        }else{
            orderDetailService.save(orderDetail);
        }
        return new AjaxResult();

    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        orderDetailService.removeById(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        orderDetailService.removeByIds(ids);
        return new AjaxResult();
    }



    /**
     * 接口：分页查询或高级查询
     * @param orderDetailDto 查询对象
     * @return IPage<OrderDetail> 分页对象
     */
    @PostMapping
    public IPage<OrderDetail> findByPage(@RequestBody OrderDetailDto orderDetailDto) {
        return orderDetailService.findByPage(orderDetailDto);
    }
}
